package com.hmall.item.web;


import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.impl.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("/list")
    public PageDTO page(Integer page, Integer size) {
        return itemService.queryPage(page, size);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
//
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        itemService.removeById(id);
    }

    @PostMapping
    public void save(@RequestBody Item item) {
        item.setCreateTime(new Date(System.currentTimeMillis()));
        itemService.save(item);
    }

    @PutMapping
    public void update(@RequestBody Item item) {
        itemService.update1(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable Long id, @PathVariable Integer status) {
        itemService.status(id, status);
    }
}
